package com.bank.backend.controller;

import com.bank.backend.annotation.AuthCheck;
import com.bank.backend.model.dto.CustomerCreateDTO;
import com.bank.backend.model.dto.CustomerQueryDTO;
import com.bank.backend.model.dto.CustomerUpdateDTO;
import com.bank.backend.model.vo.UserVO;
import com.bank.backend.result.PageResult;
import com.bank.backend.result.Result;
import com.bank.backend.service.CustomerManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * 管理员用户管理接口
 */
@RestController
@RequestMapping("/admin/customers")
@Api(tags = "管理员用户管理接口")
@Slf4j
public class CustomerManageController {

    @Resource
    private CustomerManageService customerManageService;

    /**
     * 创建客户
     * @param customerCreateDTO 客户创建DTO
     * @return 客户信息
     */
    @PostMapping
    @ApiOperation("创建客户")
    @AuthCheck(mustRole = "管理员")
    public Result createCustomer(@RequestBody CustomerCreateDTO customerCreateDTO) {
        log.info("创建客户请求参数: {}", customerCreateDTO);
        customerManageService.createCustomer(customerCreateDTO);
        return Result.success(null, "客户创建成功");
    }

    /**
     * 更新客户信息
     */
    @PutMapping
    @ApiOperation("更新客户信息")
    public Result<String> updateCustomer(@RequestBody CustomerUpdateDTO customerUpdateDTO) {
        customerManageService.updateCustomer(customerUpdateDTO);
        return Result.success(null,"客户信息更新成功");
    }

    /**
     * 分页查询客户信息
     */
    @GetMapping("/page")
    @ApiOperation("分页查询客户信息")
    public Result<PageResult<UserVO>> pageCustomers(CustomerQueryDTO queryDTO) {
        PageResult<UserVO> result = customerManageService.pageCustomers(queryDTO);
        return Result.success(result);
    }

    /**
     * 管理员重置客户账户密码
     */
    @PutMapping("/{user_id}/reset-password")
    @ApiOperation("管理员重置客户账户密码")
    public Result resetCustomerPassword(@PathVariable("user_id") Integer userId) {
        boolean success = customerManageService.resetCustomerPassword(userId);
        if (success) {
            return Result.success("密码重置成功，默认密码为888888");
        } else {
            return Result.error("重置失败，客户不存在");
        }
    }

    @DeleteMapping("/{user_id}")
    @ApiOperation("删除客户")
    @AuthCheck(mustRole = "管理员")
    public Result<String> deleteCustomer(@PathVariable("user_id") Integer userId) {
        log.info("删除客户请求参数: userId={}", userId);
        String result = customerManageService.removeCustomer(userId);
        return Result.success(null,result);
    }
} 